package br.fbv.compreja.activity.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class BaseActivity extends Activity {

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean retorno = true;
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			finish();
			break;
		default:
			retorno = super.onKeyDown(keyCode, event);
			break;
		}
		return retorno;
	}

	protected OnClickListener goToActivity(final Activity context,
			final Class<? extends Activity> clazz,
			final boolean finishCurrentActivity, final Command command) {
		OnClickListener onClickListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean blockFlow = false;
				if (command != null) {
					try {
						command.execute();
					} catch (BuzzException e) {
						blockFlow = true;
						tratarExcecao(e);
					}
				}
				if (!blockFlow) {
					Intent intent = new Intent(context, clazz);
					startActivity(intent);
					if (finishCurrentActivity) {
						context.finish();
					}
				}
			}
		};
		return onClickListener;
	}

	protected OnClickListener goToActivity(Activity context,
			Class<? extends Activity> clazz, boolean finishCurrentActivity) {
		return this.goToActivity(context, clazz, finishCurrentActivity, null);
	}

	protected OnClickListener goToActivity(Activity context,
			Class<? extends Activity> clazz) {
		return this.goToActivity(context, clazz, false, null);
	}

	protected OnClickListener goToActivity(Class<? extends Activity> clazz) {
		return this.goToActivity(this, clazz, false, null);
	}

	protected OnClickListener goToActivity(Class<? extends Activity> clazz,
			boolean finishCurrentActivity) {
		return this.goToActivity(this, clazz, finishCurrentActivity, null);
	}

	protected String getStringValue(int fieldID) {
		TextView view = (TextView) findViewById(fieldID);
		return view.getText().toString();
	}

	protected Integer getIntegerValue(int fieldID) {
		TextView view = (TextView) findViewById(fieldID);
		return Integer.valueOf(view.getText().toString());
	}

	protected boolean getBooleanValue(int fieldID) {
		CheckBox checkBox = (CheckBox) findViewById(fieldID);
		return checkBox.isChecked();
	}

	protected void showAlertBox(String msg) {
		// prepare the alert box
		AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
		// set the message to display
		alertbox.setMessage(msg);
		// add a neutral button to the alert box and assign a click
		// listener
		alertbox.setNeutralButton("Ok", null);
		// show it
		alertbox.show();
	}

	protected void tratarExcecao(Exception e) {
		Context context = getApplicationContext();
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast.makeText(context, e.getMessage(), duration);
		toast.show();
	}
}
